package net.esj.basic.core.utils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.BeanUtils;

public abstract class RegexUtils {
	
	public static boolean isEmail(String str){
        Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");  
        Matcher m = p.matcher(str);  
        return m.matches(); 
	}
	
	public static boolean matches(Pattern pattern,Object target) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		
		if(target instanceof CharSequence){
			Matcher m = pattern.matcher(target.toString());
			if(m.matches()){
				return true;
			}
		}else if(target.getClass().isArray()){
			Object[]  os = (Object[]) target;
			for(Object obj : os){
				boolean b = matches(pattern, obj);
				if(!b){
					return b;
				}
			}
		}else if(target instanceof Collection){
			Collection c = (Collection) target;
			for(Object obj : c){
				boolean b = matches(pattern, obj);
				if(!b){
					return b;
				}
			}
		}else if(target instanceof Map){
			Map map = (Map) target;
			for(Object obj : map.values()){
				boolean b = matches(pattern, obj);
				if(!b){
					return b;
				}
			}
		}else{
			PropertyDescriptor[] targetPds =BeanUtils.getPropertyDescriptors(target.getClass());
			for(PropertyDescriptor pd : targetPds){
				Object o = pd.getReadMethod().invoke(target);
				boolean b = matches(pattern, o);
				if(!b){
					return b;
				}
			}
		}
		
		return true;
		
	}
	
	/**
	 * 正则过滤器
	 * @param pattern 正则
	 * @param target 目标对象
	 * @return 符合条件的词组
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static List<String> groups(Pattern pattern,Object target) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		List<String> list = new ArrayList<String>();
		if(target instanceof CharSequence){
			list.addAll(groups(pattern, target.toString()));
			return list;
		}
		
		if(target.getClass().isArray()){
			Object[]  os = (Object[]) target;
			for(Object obj : os){
				list.addAll(groups(pattern, obj));
			}
		}else if(target instanceof Collection){
			Collection c = (Collection) target;
			for(Object obj : c){
				list.addAll(groups(pattern, obj));
			}
		}else if(target instanceof Map){
			Map map = (Map) target;
			for(Object obj : map.values()){
				list.addAll(groups(pattern, obj));
			}
		}else{
			PropertyDescriptor[] targetPds =BeanUtils.getPropertyDescriptors(target.getClass());
			for(PropertyDescriptor pd : targetPds){
				Object o = pd.getReadMethod().invoke(target);
				list.addAll(groups(pattern, o));
			}
		}
		return list;
	}
	
	public static List<String> groups(Pattern pattern,CharSequence input){
		List<String> list = new ArrayList<String>();
		Matcher m = pattern.matcher(input.toString());
		while(m.find()){
			list.add(m.group());
		}
		return list;
	}
	
	public static final void main(String[] args){
		Pattern p = Pattern.compile("(我操)");  
        Matcher m = p.matcher("我操asdasdaw瓦大厦我去&我干我干的废弃物我去萨芬的爽肤水的说法的我操"); 
        while(m.find()){
        	String v = m.group();
        	System.out.println(v);
        }
        System.out.println(Pattern.matches(".*(我操|我干).*", "asdasdaw瓦大厦我去&我干我干的废弃物我去萨芬的爽肤水的说法的"));
	}
}
